% code for averaging of SPMs wrt coherence
% Reference : Stodt J.A, 1983.
% Uses hard limiter (1 &0 )
% NOT tested

% Latest date 25.03.2003


function[SPM,ProcDef] = cohguess(SPMat,ProcDef,m,i),

A = size(SPMat);
trsh = ProcDef.CohTr;

if trsh < 0 | trsh > 1,
   fprintf('cohspm-> Warning trsh is reset to 1.0\n');
   fprintf('         No change from this module\n'); 
   trsh=1;
end;

CHxEy = ocoh(SPMat,5,1); % returns ordinary coherency 
CHyEx = ocoh(SPMat,4,2); % between H & E fields; 2D array [nfrq len]
%CHxEy = pcoh(SPMatrix,ProcDef,'Ex'); % returns ordinary coherency 
%CHyEx = pcoh(SPMatrix,ProcDef,'Ey'); % between H & E fields; 2D array [nfrq len]
C = sqrt(CHxEy.*CHyEx);


if m == 1, % Preferencial stacking based on coherence
		h = sort(C);
		tre = h(floor((0.8)*A(2)));
		k = find(C<tre);
		ProcDef.Weights(i,k) = 0;
elseif m == 2, % Coherence trashold stacking
      k = find(C<trsh);
      if A(2)-length(k) > 4,
         ProcDef.Weights(i,k) = 0;
      end;
end;
	SPM = WeightSPM(SPMat,ProcDef,i);
